package boundary;
import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

import control.*;

import java.util.ArrayList;
import java.util.Random;

import entity.*;
import entity.Box;


/**
 * The Class MainPanel.
 */
public class MainPanel extends JPanel implements ActionListener  {

	private static final long serialVersionUID = 5L;
	public static BoardPanel boardPanel = new BoardPanel();
	public static InventoryPanel inventory = new InventoryPanel();
	private static ItemController itemControl = new ItemController();
	private static CardController cardControl = new CardController();
	private static PeciesController peciesControl = new PeciesController();
	private static ChartController chartControl = new ChartController();
	public Boolean waitDice = false;
	public Boolean haveDiceValue = false;
	private Timer timer,roundTimer;
	private int DICE_WAIT_TIME;
	private Box box = new Box();
	private boolean keyPress = false;
	private int i =0;
	private boolean ischinese=true;
	private JFrame frame;
	private JPanel panel=new JPanel();
	private int roundTemp =0,roundNum=0;
	private ArrayList<Pecies> al = peciesControl.getAllPecies();
	
	public MainPanel(JFrame frame,boolean ischinese) {
		this.ischinese=ischinese;
		this.frame=frame;
		MainPanel.itemControl.createItemCards(this.ischinese);
		MainPanel.cardControl.createCard(this.ischinese);
        MainPanel.chartControl.setLangauge(this.ischinese);
		InventoryPanel.setLanguage(ischinese);
		
		this.setFocusable(true);
		panel.addKeyListener(new TAdapter());
		panel.setFocusable(true);
		panel.setBackground(Color.WHITE);
		panel.setDoubleBuffered(true);
		DICE_WAIT_TIME = new Random().nextInt(3000)+1000;
		timer = new Timer(DICE_WAIT_TIME, this);
		roundTimer = new Timer(20000,this);
		roundTimer.start();
		panel.setLayout(new GridBagLayout()); 
		GridBagConstraints c = new GridBagConstraints(); 
		/**make this component tall*/
		c.fill = GridBagConstraints.HORIZONTAL;     c.ipady = 600;     
		c.weightx = 0.0;     c.gridwidth = 600;     c.gridx = 0;     c.gridy = 0;
		panel.add((JPanel)boardPanel,c);
		/**make this component tall*/
		c.fill = GridBagConstraints.HORIZONTAL;     c.ipady = 100;     
		c.weightx = 600.0;     c.gridwidth = 600;     c.gridx = 0;     c.gridy = 1;
		panel.add((JPanel)inventory,c); 
		frame.setContentPane(panel);
		frame.validate();
		frame.repaint();
		frame.setVisible( false );
		frame.setVisible( true );
		frame.setState(Frame.NORMAL);
	}
	public MainPanel(boolean ischinese) {
		System.out.println("abc");
		this.ischinese=ischinese;
		MainPanel.itemControl.createItemCards(this.ischinese);
		MainPanel.cardControl.createCard(this.ischinese);
		InventoryPanel.setLanguage(ischinese);

		this.setFocusable(true);
		panel.addKeyListener(new TAdapter());
		panel.setFocusable(true);
		panel.setBackground(Color.WHITE);
		panel.setDoubleBuffered(true);
		DICE_WAIT_TIME = new Random().nextInt(3000)+1000;
		timer = new Timer(DICE_WAIT_TIME, this);
		roundTimer = new Timer(20000,this);
		roundTimer.start();
		panel.setLayout(new GridBagLayout()); 
		GridBagConstraints c = new GridBagConstraints(); 
		/**make this component tall*/
		c.fill = GridBagConstraints.HORIZONTAL;     c.ipady = 600;  
		c.weightx = 0.0;     c.gridwidth = 600;     c.gridx = 0;     c.gridy = 0;
		panel.add((JPanel)boardPanel,c);
		/**make this component tall*/
		c.fill = GridBagConstraints.HORIZONTAL;     c.ipady = 100; 
		c.weightx = 600.0;     c.gridwidth = 600;     c.gridx = 0;     c.gridy = 1;
		panel.add((JPanel)inventory,c); 
		frame.setContentPane(panel);
		frame.validate();
		frame.repaint();
		frame.setVisible( false );
		frame.setVisible( true );
		frame.setState(Frame.NORMAL);
	}
	public void actionPerformed(ActionEvent e) {
		if(keyPress){
			 if(waitDice) {  
				 	
				 	if(haveDiceValue) {
				 		Pecies moveObj = boardPanel.getMoveObj();
						int boardX= boardPanel._x;
						int boardY = boardPanel._y;
						int moveObjX = moveObj.getX();
						int moveObjY = moveObj.getY();
						
						int boxX = box.getX(moveObj.getBoxNum());
						int boxY = box.getY(moveObj.getBoxNum());
						if((Math.abs(boardX)+moveObjX > boxX-5 && Math.abs(boardX)+moveObjX < boxX+5) && (Math.abs(boardY)+moveObjY > boxY-5 && Math.abs(boardY)+moveObjY < boxY+5)) { 
							waitDice = false;
							haveDiceValue = false;
							
						}
				 	}
				 	else {
				 		int diceValue  = inventory.stopRadomDice();
				 		haveDiceValue = true;
						
						boardPanel.startMove();
						Pecies moveObj = boardPanel.getMoveObj();
						moveObj.setCurrentBoxNum(diceValue);
						timer.stop();
						timer = new Timer(15,this);
						timer.start();
				 	}				
				}
			else {
				Pecies moveObj = boardPanel.getMoveObj();
				
				char cardType = box.getType(moveObj.getBoxNum());
				if(cardType != 'N' && cardType !='I'){
					boardPanel.stopMove();
					EventLog event = new EventLog(cardType,roundNum);
					int effect = cardControl.drawCard(cardType,event);
					if(moveObj.isItemUsing())
						effect = itemControl.useItemEffect(effect, moveObj);
					moveObj.setSelf(effect);
					event.setSelf(moveObj.getSelf());
					moveObj.setEventLog(event);
					boardPanel.startMove();
					inventory.doRepaint();
				}
				
				else if(cardType == 'I'){
					boardPanel.stopMove();
					itemControl.displayItemCard(moveObj);
					boardPanel.startMove();
					inventory.doRepaint();
				}
				
				peciesControl.checkEndGame(moveObj);
				
				itemControl.UpdateRoundUse(moveObj);
				waitDice = false;
				
				 if(boardPanel.getTimer().isRunning()) {
					 boardPanel.shiftToken();
					 itemControl.setItemClickable(boardPanel, inventory);
				}
				DICE_WAIT_TIME = new Random().nextInt(3000)+1000;
				timer.stop();
				

				timer = new Timer(DICE_WAIT_TIME,this);
				keyPress = false;
				roundTimer = new Timer(20000,this);
				roundTimer.start();
			}
		}
		else{//no key Press event trigger 
			roundTemp++;
			if((roundTemp)%(al.size()) == 0){
				roundNum++;
				System.out.println("Round:"+roundNum);
			}
			roundTimer.stop();
			keyPress = true;
			waitDice = true;
			inventory.startRadomDice();
			timer.start();
		
			i++;
		}

		
    }
	
	private class TAdapter extends KeyAdapter {

        public void keyReleased(KeyEvent e) {
			if(!waitDice) {
				keyPress = true;
				waitDice = true;
				roundTimer.stop();
				inventory.startRadomDice();
				timer.start();
				roundTemp++;
				if((roundTemp)%(al.size()) == 0){
					roundNum++;
				}		
			}
        }

        public void keyPressed(KeyEvent e) {
        }
    }
		
}
